<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="制单日期">
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择开始时间" class="query-group-cust" v-model="queryParam.createTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date :show-time="true" date-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择结束时间" class="query-group-cust" v-model="queryParam.createTime_end"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('采购合同评审单管理')">导出</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl" @change="handleImportExcel">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>


        <a-tabs v-model="activeKey" @change="handleChangeTab">
          <a-tab-pane key="temporarySave" tab="暂存中">
            <a-table
              ref="temporarySave"
              size="middle"
              bordered
              rowKey="id"
              class="j-table-force-nowrap1"
              :scroll="{ x: true }"
              :columns="columns"
              :dataSource="temporarySave.dataSource"
              :pagination="temporarySave.ipagination"
              :loading="temporarySave.loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            >
              <template slot="htmlSlot" slot-scope="text">
                <div v-html="text"></div>
              </template>
              <template slot="imgSlot" slot-scope="text, record">
                <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
                <img
                  v-else
                  :src="getImgView(text)"
                  :preview="record.id"
                  height="25px"
                  alt=""
                  style="max-width: 80px; font-size: 12px; font-style: italic"
                />
              </template>
              <template slot="fileSlot" slot-scope="text">
                <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
                <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                  下载
                </a-button>
              </template>

              <span slot="action" slot-scope="text, record">
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical"></a-divider>
              <act-apply-btn
                v-show="record.status < 0"
                @success="loadData"
                :data-id="record.id"
                :serviceName="serviceName"
                :variables="record"
              ></act-apply-btn>
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
            </a-table>
          </a-tab-pane>
          <a-tab-pane key="needEdit" tab="需修改">
            <a-table
              ref="needEditTable"
              size="middle"
              bordered
              rowKey="id"
              class="j-table-force-nowrap1"
              :scroll="{ x: true }"
              :columns="columns"
              :dataSource="needEditTable.dataSource"
              :pagination="needEditTable.ipagination"
              :loading="needEditTable.loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            >
              <template slot="htmlSlot" slot-scope="text">
                <div v-html="text"></div>
              </template>
              <template slot="imgSlot" slot-scope="text, record">
                <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
                <img
                  v-else
                  :src="getImgView(text)"
                  :preview="record.id"
                  height="25px"
                  alt=""
                  style="max-width: 80px; font-size: 12px; font-style: italic"
                />
              </template>
              <template slot="fileSlot" slot-scope="text">
                <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
                <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                  下载
                </a-button>
              </template>

              <span slot="action" slot-scope="text, record">
              <a v-if="record.status < 0" @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical"></a-divider>
              <act-apply-btn
                v-show="record.actStatus == '撤回'"
                @success="loadData"
                :data-id="record.id"
                :serviceName="serviceName"
                :variables="record"
              ></act-apply-btn>
              <a-popconfirm  v-if="record.actStatus != '撤回'" title="确定取消申请吗?" @confirm="() => handleStop(record)">
               <a href="javascript:;">取消申请</a>
              </a-popconfirm>
              <a-divider v-if="record.actStatus != '撤回'" type="vertical" />
              <a v-if="record.actStatus !='撤回'" @click="handleProcess(record)">提交</a>
              <a-divider v-if="record.actStatus != '撤回'" type="vertical" />
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
            </a-table>
          </a-tab-pane>
          <a-tab-pane key="approving" tab="审批中">
            <a-table
              ref="approvingTable"
              size="middle"
              bordered
              rowKey="id"
              class="j-table-force-nowrap1"
              :scroll="{ x: true }"
              :columns="columns"
              :dataSource="approvingTable.dataSource"
              :pagination="approvingTable.ipagination"
              :loading="approvingTable.loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            >
              <template slot="htmlSlot" slot-scope="text">
                <div v-html="text"></div>
              </template>
              <template slot="imgSlot" slot-scope="text, record">
                <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
                <img
                  v-else
                  :src="getImgView(text)"
                  :preview="record.id"
                  height="25px"
                  alt=""
                  style="max-width: 80px; font-size: 12px; font-style: italic"
                />
              </template>
              <template slot="fileSlot" slot-scope="text">
                <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
                <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                  下载
                </a-button>
              </template>

              <span slot="action" slot-scope="text, record">
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a-popconfirm  v-if="record.actStatus != '撤回'" title="确定取消申请吗?" @confirm="() => handleStop(record)">
                      <a href="javascript:;">取消申请</a>
                    </a-popconfirm>
                  </a-menu-item>
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
            </a-table>
          </a-tab-pane>
          <a-tab-pane key="approved" tab="已审批">
            <a-table
              ref="approvedTable"
              size="middle"
              bordered
              rowKey="id"
              class="j-table-force-nowrap1"
              :scroll="{ x: true }"
              :columns="columns"
              :dataSource="approvedTable.dataSource"
              :pagination="approvedTable.ipagination"
              :loading="approvedTable.loading"
              :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
            >
              <template slot="htmlSlot" slot-scope="text">
                <div v-html="text"></div>
              </template>
              <template slot="imgSlot" slot-scope="text, record">
                <span v-if="!text" style="font-size: 12px; font-style: italic">无图片</span>
                <img
                  v-else
                  :src="getImgView(text)"
                  :preview="record.id"
                  height="25px"
                  alt=""
                  style="max-width: 80px; font-size: 12px; font-style: italic"
                />
              </template>
              <template slot="fileSlot" slot-scope="text">
                <span v-if="!text" style="font-size: 12px; font-style: italic">无文件</span>
                <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
                  下载
                </a-button>
              </template>

              <span slot="action" slot-scope="text, record">
              <act-historic-detail-btn :data-id="record.id"></act-historic-detail-btn>
              <a-divider type="vertical" />
              <a-dropdown>
                <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
                <a-menu slot="overlay">
                  <a-menu-item>
                    <a @click="handleDetail(record)">详情</a>
                  </a-menu-item>
                  <a-menu-item>
                    <a v-if="activeKey=='approved'" @click="handleDayin(record)">打印</a>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
            </a-table>
          </a-tab-pane>
        </a-tabs>
    </div>

    <buss-material-procurement-contract-review-modal ref="modalForm" @ok="modalFormOk"></buss-material-procurement-contract-review-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import BussMaterialProcurementContractReviewModal from './modules/BussMaterialProcurementContractReviewModal'
  import {filterMultiDictText} from '@/components/dict/JDictSelectUtil'
  import ActApplyBtn from '@views/flowable/components/ActApplyBtn.vue'
  import ActHistoricDetailBtn from '@views/flowable/components/ActHistoricDetailBtn.vue'
  import { getAction } from '@api/manage'

  export default {
    name: 'BussMaterialProcurementContractReviewList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      ActHistoricDetailBtn, ActApplyBtn,
      BussMaterialProcurementContractReviewModal
    },
    data () {
      return {

        activeKey: 'temporarySave',
        serviceName: 'bussMaterialProcurementContractReviewService',

        temporarySave:{
          loading: false,
          dataSource:[],
          columns:[],
          ipagination: {
            current: 1,
            pageSize: 30,
            pageSizeOptions: ['10', '30', '100'],
            showTotal: (total, range) => {
              return range[0] + '-' + range[1] + ' 共' + total + '条'
            },
            showQuickJumper: true,
            showSizeChanger: true,
            total: 0,
          },
        },
        needEditTable:{
          loading: false,
          dataSource:[],
          columns:[],
          ipagination: {
            current: 1,
            pageSize: 30,
            pageSizeOptions: ['10', '30', '100'],
            showTotal: (total, range) => {
              return range[0] + '-' + range[1] + ' 共' + total + '条'
            },
            showQuickJumper: true,
            showSizeChanger: true,
            total: 0,
          },
        },
        approvingTable: {
          loading: false,
          dataSource: [],
          // 表头
          ipagination: {
            current: 1,
            pageSize: 3000,
            pageSizeOptions: ['3000', '200', '500'],
            showTotal: (total, range) => {
              return range[0] + '-' + range[1] + ' 共' + total + '条'
            },
            showQuickJumper: true,
            showSizeChanger: true,
            total: 0,
          },
        },
        approvedTable: {
          loading: false,
          dataSource: [],
          // 表头
          ipagination: {
            current: 1,
            pageSize: 3000,
            pageSizeOptions: ['3000', '200', '500'],
            showTotal: (total, range) => {
              return range[0] + '-' + range[1] + ' 共' + total + '条'
            },
            showQuickJumper: true,
            showSizeChanger: true,
            total: 0,
          },
        },
        description: '采购合同评审单管理管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'单据编号',
            align:"center",
            dataIndex: 'code'
          },
          {
            title:'单据名称',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'单据类型',
            align:"center",
            dataIndex: 'type_dictText'
          },
          {
            title:'供应商名称',
            align:"center",
            dataIndex: 'supplierName'
          },
          {
            title:'备注',
            align:"center",
            dataIndex: 'remark'
          },
          {
            title:'制单人',
            align:"center",
            dataIndex: 'createBy'
          },
          {
            title:'制单日期',
            align:"center",
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/procurement/bussMaterialProcurementContractReview/list",
          delete: "/procurement/bussMaterialProcurementContractReview/delete",
          deleteBatch: "/procurement/bussMaterialProcurementContractReview/deleteBatch",
          exportXlsUrl: "/procurement/bussMaterialProcurementContractReview/exportXls",
          importExcelUrl: "procurement/bussMaterialProcurementContractReview/importExcel",

        },
        dictOptions:{},
        superFieldList:[],
      }
    },
    created() {
      this.handleChangeTab()
      this.getSuperFieldList();
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      },
    },
    methods: {
      handleDayin(row) {
        let ids = row.id
        window.open(`${window._CONFIG['domianURL']}/jmreport/view/1136439102377385984?dataId=${ids}`,'1' ,'width=1000','height=500','_parent')
      },
      handleStop(row) {
        const params = {
          instanceId: row.processInstanceId,
        }
        stopProcess(params).then((res) => {
          this.$message.success(res.message)
          this.handleChangeTab()
        })
      },
      searchQuery() {
        if(typeof this.beforeSearch === 'function'){
          let isreturn = this.beforeSearch()
          if(!isreturn){
            return;
          }
        }
        this.handleChangeTab();
      },

      searchReset(){
        this.queryParam = [];
        this.handleChangeTab();
      },
      handleChangeTab() {
        switch (this.activeKey) {
          case 'temporarySave':
            this.loadTempSaveData()
            break
          case 'needEdit':
            this.loadNeedEditData()
            break
          case 'approving':
            this.loadApprovingData()
            break
          case 'approved':
            this.loadApprovedData()
            break
          default:
            break
        }
      },

      loadTempSaveData() {
        //暂存中单据，status=-1
        this.queryParam.status = -2
        this.temporarySave.loading = true
        var params = this.getQueryParams();//查询条件
        getAction(this.url.list, params)
          .then((res) => {
            if (res.success) {
              this.temporarySave.dataSource = res.result.records || res.result
              if (res.result.total) {
                this.temporarySave.ipagination.total = res.result.total
              } else {
                this.temporarySave.ipagination.total = 0
              }
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.temporarySave.loading = false
          })
      },
      loadNeedEditData() {
        //需修改单据，status=-1
        this.queryParam.status = -1
        this.needEditTable.loading = true
        getAction(this.url.list, this.queryParam)
          .then((res) => {
            if (res.success) {
              this.needEditTable.dataSource = res.result.records || res.result
              if (res.result.total) {
                this.needEditTable.ipagination.total = res.result.total
              } else {
                this.needEditTable.ipagination.total = 0
              }
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.needEditTable.loading = false
          })
      },
      loadApprovingData() {
        //审核中单据，status=0
        this.queryParam.status = 0
        this.approvingTable.loading = true
        getAction(this.url.list, this.queryParam)
          .then((res) => {
            if (res.success) {
              this.approvingTable.dataSource = res.result.records || res.result
              if (res.result.total) {
                this.approvingTable.ipagination.total = res.result.total
              } else {
                this.approvingTable.ipagination.total = 0
              }
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.approvingTable.loading = false
          })
      },
      loadApprovedData() {
        //已审核单据，status=999
        this.queryParam.status = 999
        this.approvedTable.loading = true
        getAction(this.url.list, this.queryParam)
          .then((res) => {
            if (res.success) {
              this.approvedTable.dataSource = res.result.records || res.result
              if (res.result.total) {
                this.approvedTable.ipagination.total = res.result.total
              } else {
                this.approvedTable.ipagination.total = 0
              }
            } else {
              this.$message.warning(res.message)
            }
          })
          .finally(() => {
            this.approvedTable.loading = false
          })
      },
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'code',text:'单据编号',dictCode:''})
        fieldList.push({type:'string',value:'name',text:'单据名称',dictCode:''})
        fieldList.push({type:'string',value:'type',text:'单据类型',dictCode:'file_type'})
        fieldList.push({type:'sel_search',value:'supplierId',text:'供应商名称',dictTable:"erp_supplier", dictText:'name', dictCode:'id'})
        fieldList.push({type:'Text',value:'remark',text:'备注',dictCode:''})
        fieldList.push({type:'Text',value:'contractEditor',text:'合同编辑',dictCode:''})
        fieldList.push({type:'Text',value:'filePath',text:'附件',dictCode:''})
        fieldList.push({type:'string',value:'createBy',text:'制单人',dictCode:''})
        fieldList.push({type:'datetime',value:'createTime',text:'制单日期'})
        this.superFieldList = fieldList
      }
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>